package com.jiayang.hhotel.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.AgreeGuestModel;
import com.jiayang.hhotel.util.LogUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/5/16 0016.
 * 协议客户列表适配器
 */

public class AgreeGuestListAdapter extends SwipeMenuAdapter<AgreeGuestListAdapter.ViewHolder> {

//    private static final int TYPE_ITEM =0; //普通Item View
//    private static final int TYPE_FOOTER = 1; // 底部FootView

    private List<AgreeGuestModel> mList;
    private Context t;

    private btClickListener listener;

    private HashMap<AgreeGuestModel,Boolean> missMap;//用于存放已点丢失的item

    public AgreeGuestListAdapter(Context context, List<AgreeGuestModel> list, btClickListener listener){
        this.mList=list;
        this.t=context;
        this.listener=listener;
        this.missMap=new HashMap<>();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        AgreeGuestModel model=mList.get(position);
        holder.name.setText(model.getName());
        holder.rmNum.setText(t.getString(R.string.room_num)+model.getId());
        holder.industry.setText(t.getString(R.string.room_name)+model.getIndustry());
        holder.sale.setText(t.getString(R.string.person_count)+model.getSale());
        holder.contactName.setText(model.getContactName());
        holder.contactTel.setText(model.getContactTel()+"");
        if(missMap.get(model)!=null){
            holder.miss.setText(t.getString(R.string.already_miss));
        }else{
            holder.miss.setText(t.getString(R.string.miss));
        }
        holder.bind(model,position);
    }

    public void setMissList(int position){
        if(missMap==null){
            missMap=new HashMap<>();
        }
        missMap.put(mList.get(position),true);
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(t).inflate(R.layout.agree_guest_list_item, parent, false);
    }

    @Override
    public ViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new ViewHolder(realContentView);
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView name;//名称
        TextView rmNum;//编号
        TextView industry;//行业
        TextView sale;//销售员
        TextView contactName;//联系人
        TextView contactTel;//联系电话

        Button edit;//编辑
        Button miss;//丢失
        Button seeDetails;//查看详情

        public ViewHolder(View convertView) {
            super(convertView);
            name= (TextView) convertView.findViewById(R.id.tv_name);
            rmNum= (TextView) convertView.findViewById(R.id.tv_id);
            industry= (TextView) convertView.findViewById(R.id.tv_industry);
            sale= (TextView) convertView.findViewById(R.id.tv_sale);
            contactName= (TextView) convertView.findViewById(R.id.tv_contact_name);
            contactTel= (TextView) convertView.findViewById(R.id.tv_contact_tel);

            edit= (Button) convertView.findViewById(R.id.bt_editor);
            miss= (Button) convertView.findViewById(R.id.bt_miss);
            seeDetails= (Button) convertView.findViewById(R.id.bt_see_details);
        }

        private int position;

        private void bind(AgreeGuestModel model,int pos){
            this.position=pos;
            edit.setTag(model);
            miss.setTag(model);
            seeDetails.setTag(model);

            edit.setOnClickListener(this);
            miss.setOnClickListener(this);
            seeDetails.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.bt_editor:
                    if(listener!=null){
                        listener.onBTClickListener((AgreeGuestModel) v.getTag(), R.id.bt_editor,position);
                    }
                    break;
                case R.id.bt_miss:
                    if(listener!=null&&missMap.get(miss.getTag())==null){
                        listener.onBTClickListener((AgreeGuestModel) v.getTag(), R.id.bt_miss,position);
                    }
                    break;
                case R.id.bt_see_details:
                    if(listener!=null){
                        listener.onBTClickListener((AgreeGuestModel) v.getTag(), R.id.bt_see_details,position);
                    }
                    break;
            }
        }
    }

    public interface btClickListener{
        void onBTClickListener(AgreeGuestModel model, int id, int position);
    }
}
